package com.hruiworks.campusgroup.controller;

import com.hruiworks.campusgroup.common.BaseResponse;
import com.hruiworks.campusgroup.common.PageVO;
import com.hruiworks.campusgroup.enums.ErrorCode;
import com.hruiworks.campusgroup.enums.SuccessCode;
import com.hruiworks.campusgroup.exception.BusinessException;
import com.hruiworks.campusgroup.pojo.entity.Activity;
import com.hruiworks.campusgroup.pojo.entity.ActivityGrade;
import com.hruiworks.campusgroup.pojo.entity.Administrator;
import com.hruiworks.campusgroup.pojo.entity.Feedback;
import com.hruiworks.campusgroup.pojo.query.ActivityQuery;
import com.hruiworks.campusgroup.pojo.request.*;
import com.hruiworks.campusgroup.pojo.vo.AdministratorInfoVo;
import com.hruiworks.campusgroup.service.ActivityGradeService;
import com.hruiworks.campusgroup.service.ActivityService;
import com.hruiworks.campusgroup.service.AdministratorService;
import com.hruiworks.campusgroup.service.FeedbackService;
import com.hruiworks.campusgroup.utils.AuthorityUtils;
import com.hruiworks.campusgroup.utils.ResultUtils;
import com.hruiworks.usercheck.pojo.entity.JwtEntity;
import com.hruiworks.usercheck.util.ParamUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * 管理员相关controller
 *
 * @author jiushui
 */
@RestController
@RequestMapping("/api/administrator")
public class AdministratorController {

    @Resource
    AdministratorService administratorService;

    @Resource
    ActivityService activityService;

    @Resource
    private ActivityGradeService activityGradeService;

    @Resource
    private FeedbackService feedbackService;

    /**
     * 创建管理员
     *
     * @param administratorCreateRequest 管理员创建请求体
     * @param httpServletRequest         http请求体
     * @return 所创建的管理员信息
     */
    @PostMapping("/create")
    public BaseResponse<AdministratorInfoVo> createAdministrator(@RequestBody AdministratorCreateRequest administratorCreateRequest,
                                                                 HttpServletRequest httpServletRequest) {
        Long administratorId = AuthorityUtils.currentAdministrator(httpServletRequest).getId();

        AdministratorInfoVo administratorInfoVo = administratorService.createAdministrator(administratorCreateRequest, administratorId);

        return ResultUtils.success(administratorInfoVo, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 管理员登录
     *
     * @param administratorLoginRequest 用户登录需要的信息
     * @return 返回jwt凭证
     */
    @PostMapping("/login")
    public BaseResponse<JwtEntity> administratorLogin(@RequestBody AdministratorLoginRequest administratorLoginRequest) {
        if (Objects.isNull(administratorLoginRequest) || ParamUtils.isAnyBlank(administratorLoginRequest)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        JwtEntity jwtEntity = administratorService.administratorLogin(administratorLoginRequest.getUsername(), administratorLoginRequest.getPassword());
        return ResultUtils.success(jwtEntity, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 删除管理员
     *
     * @param administratorId    要删除管理员的id
     * @param httpServletRequest http请求体
     * @return 删除结果
     */
    @GetMapping("/delete")
    public BaseResponse<Boolean> deleteAdministrator(@RequestParam("administrator_id") Long administratorId,
                                                     HttpServletRequest httpServletRequest) {
        Long currentAdministratorId = AuthorityUtils.currentAdministrator(httpServletRequest).getId();
        boolean result = administratorService.deleteAdministrator(currentAdministratorId, administratorId);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }


    /**
     * 更新管理员信息
     *
     * @param administratorUpdateRequest 更新请求体
     * @param httpServletRequest         http请求体
     * @return 更新情况
     */
    @PostMapping("/update/password")
    public BaseResponse<Integer> updateAdministratorPassword(@RequestBody AdministratorUpdateRequest administratorUpdateRequest,
                                                             HttpServletRequest httpServletRequest) {
        Long currentAdministratorId = AuthorityUtils.currentAdministrator(httpServletRequest).getId();
        Integer result = administratorService.updateAdministrator(administratorUpdateRequest, currentAdministratorId);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }


    /**
     * 获取当前管理员的脱敏信息
     *
     * @param httpServletRequest http请求体
     * @return 管理员脱敏信息
     */
    @GetMapping("/current")
    public BaseResponse<AdministratorInfoVo> currentAdministrator(HttpServletRequest httpServletRequest) {
        Long administratorId = AuthorityUtils.currentAdministrator(httpServletRequest).getId();
        AdministratorInfoVo currentAdministratorInfo = administratorService.getCurrentAdministratorInfo(administratorId);
        return ResultUtils.success(currentAdministratorInfo, SuccessCode.COMMON_SUCCESS);
    }

    @PostMapping("/activity/list")
    public BaseResponse<List<Activity>> list(@RequestBody ActivityQuery activityQuery, HttpServletRequest httpServletRequest) {
        Administrator administrator = AuthorityUtils.currentAdministrator(httpServletRequest);
        List<Activity> activities = activityService.listActivity(activityQuery, administrator);
        return ResultUtils.success(activities, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 查看所管辖的管理员列表
     *
     * @param httpServletRequest http请求体
     * @return 脱敏信息列表
     */
    @GetMapping("/management/list")
    public BaseResponse<List<AdministratorInfoVo>> listAdministrator(@RequestParam("search_value") String searchValue,
                                                                         HttpServletRequest httpServletRequest) {
        Administrator administrator = AuthorityUtils.currentAdministrator(httpServletRequest);
        List<AdministratorInfoVo> administratorInfoVos = administratorService.listAdministratorInfo(administrator, searchValue);
        return ResultUtils.success(administratorInfoVos, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 审核活动
     *
     * @param activityId         活动id
     * @param reviewStatus       活动意见
     * @param httpServletRequest http请求体
     * @return 操作结果
     */
    @GetMapping("/activity/review")
    public BaseResponse<Boolean> reviewActivity(@RequestParam("activity_id") Long activityId,
                                                @RequestParam("review_status") Integer reviewStatus,
                                                HttpServletRequest httpServletRequest) {
        Administrator administrator = AuthorityUtils.currentAdministrator(httpServletRequest);
        Boolean result = activityService.reviewActivity(activityId, reviewStatus, administrator);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 更新管理员
     *
     * @param activityUpdateRequest 活动更新请求体
     * @param httpServletRequest    http请求体
     * @return 操作结果
     */
    @PostMapping("/activity/update")
    public BaseResponse<Boolean> updateActivity(@RequestBody ActivityUpdateRequest activityUpdateRequest, HttpServletRequest httpServletRequest) {
        Administrator administrator = AuthorityUtils.currentAdministrator(httpServletRequest);
        Boolean result = activityService.updateActivity(activityUpdateRequest, administrator);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 管理员创建活动
     *
     * @param activityCreateRequest 创建活动请求体
     * @param httpServletRequest    http请求体
     * @return 操作结果
     */
    @PostMapping("/activity/create")
    public BaseResponse<Boolean> createActivity(@RequestBody ActivityCreateRequest activityCreateRequest, HttpServletRequest httpServletRequest) {
        Administrator administrator = AuthorityUtils.currentAdministrator(httpServletRequest);
        Boolean result = activityService.createActivity(activityCreateRequest, administrator);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }

    @GetMapping("/activity/delete")
    public BaseResponse<Boolean> deleteActivity(@RequestParam("activity_id") Long activityId, HttpServletRequest httpServletRequest) {
        Administrator administrator = AuthorityUtils.currentAdministrator(httpServletRequest);
        Boolean result = activityService.deleteActivity(activityId, administrator);
        return ResultUtils.success(result, SuccessCode.COMMON_SUCCESS);
    }


    /**
     * 分页获取整个活动的评分
     *
     * @param current
     * @param pageSize
     * @param activityId
     * @return
     */
    @GetMapping("/grade/all/page")
    public BaseResponse<PageVO<ActivityGrade>> pageAllGrade(@RequestParam("current") Long current,
                                                            @RequestParam("page_size") Long pageSize,
                                                            @RequestParam("activity_id") Long activityId) {
        PageVO<ActivityGrade> activityGradePageVO = activityGradeService.pageActivityGrade(current, pageSize, activityId);
        return ResultUtils.success(activityGradePageVO, SuccessCode.COMMON_SUCCESS);
    }

    /**
     * 分页获取整个活动的反馈
     *
     * @param current
     * @param pageSize
     * @param activityId
     * @return
     */
    @GetMapping("/feedback/all/page")
    public BaseResponse<PageVO<Feedback>> pageAllFeedback(@RequestParam("current") Long current,
                                                          @RequestParam("page_size") Long pageSize,
                                                          @RequestParam("activity_id") Long activityId) {
        PageVO<Feedback> feedbackPageVO = feedbackService.pageFeedback(current, pageSize, activityId);
        return ResultUtils.success(feedbackPageVO, SuccessCode.COMMON_SUCCESS);
    }
}
